package com.genghis.rapidcrm.web.view;

import com.genghis.rapidcrm.kernel.RapidLinkModel;
import com.genghis.rapidcrm.kernel.RapidLinkModelProvider;
import com.genghis.rapidcrm.kernel.UsersRecordModel;
import com.genghis.rapidcrm.kernel.model.RapidBaseModel;
import com.genghis.rapidcrm.web.view.model.RapidCssScriptModel;
import com.genghis.rapidcrm.web.view.model.RapidJsScriptModel;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class RapidHeaderView extends RapidControllerView {

    //Note : To get the right hook for immediate parent in PHP,
    // specially in case of deep hierarchy
	/*function preProcessParentTplName(Vtiger_Request $request) {
		return parent::preProcessTplName($request);
	}*/

    /**
     * Function to get the list of Header Links
     *
     * @return <Array> - List of Vtiger_Link_Model instances
     */
    protected List<RapidLinkModel> getHeaderLinks(HttpServletRequest request) {
        UsersRecordModel userModel = UsersRecordModel.getCurrentUserModel(request);
        String userEmail = userModel.getEmail();

        List<RapidLinkModel> headerLinks = new ArrayList<>();
        // Note: This structure is expected to generate side-bar feedback button.
        RapidLinkModel link1 = new RapidLinkModel(3);
        link1.setLinkType("HEADERLINK");
        link1.setLinkLabel("LBL_FEEDBACK");
        link1.setLinkUrl(String.format("javascript:window.open('http://www.ccjicheng.com/changjianwenti/xiaoshou/" +
                        "?version=%s&email=%s&uid=%s&ui=6','feedbackwin','height=400,width=550,top=200,left=300')",
                "caropCurrentVersion", userEmail, "appUniqueKey"));
        link1.setLinkIcon("info.png");
        RapidLinkModel subLink11 = new RapidLinkModel();
        subLink11.setLinkType("HEADERLINK");
        subLink11.setLinkLabel("LBL_DOCUMENTATION");
        subLink11.setLinkUrl("http://www.ccjicheng.com/xinwenzhongxin/qiyewenku/");
        subLink11.setLinkIcon("");
        subLink11.setTarget("_blank");
        RapidLinkModel subLink12 = new RapidLinkModel();
        subLink12.setLinkType("HEADERLINK");
        subLink12.setLinkLabel("LBL_VIDEO_TUTORIAL");
        subLink12.setLinkUrl("http://www.ccjicheng.com/xinwenzhongxin/yingxiaoguanli/");
        subLink12.setLinkIcon("");
        subLink12.setTarget("_blank");
        // Note: This structure is expected to generate side-bar feedback button.
        RapidLinkModel subLink13 = new RapidLinkModel();
        subLink13.setLinkType("HEADERLINK");
        subLink13.setLinkLabel("LBL_FEEDBACK");
        subLink13.setLinkUrl(String.format("javascript:window.open('http://www.ccjicheng.com/changjianwenti/xiaoshou/" +
                        "?version=%s&email=%s&uid=%s&ui=6','feedbackwin','height=400,width=550,top=200,left=300')",
                "caropCurrentVersion", userEmail, "appUniqueKey"));
        subLink13.setLinkIcon("");
        link1.addChildLink(subLink11).addChildLink(subLink12).addChildLink(subLink13);
        headerLinks.add(link1);

        String appUrlPrefix = getAppConfig().getProperty("rapidcrm.url-prefix", "");
        if (userModel.isAdminUser()) {
            RapidLinkModel crmSettingsLinknew = new RapidLinkModel(3);
            crmSettingsLinknew.setLinkType("HEADERLINK");
            crmSettingsLinknew.setLinkLabel("LBL_ERP_SETTINGS");
            crmSettingsLinknew.setLinkUrl("");
            crmSettingsLinknew.setLinkIcon("setting.png");
            RapidLinkModel subLink21 = new RapidLinkModel();
            subLink21.setLinkType("HEADERLINK");
            subLink21.setLinkLabel("LBL_APP_SETTINGS");
            subLink21.setLinkUrl(appUrlPrefix + "/settings");
            subLink21.setLinkIcon("");
            RapidLinkModel subLink22 = new RapidLinkModel();
            RapidLinkModel subLink23 = new RapidLinkModel();
            subLink23.setLinkType("HEADERLINK");
            subLink23.setLinkLabel("LBL_MANAGE_USERS");
            subLink23.setLinkUrl(appUrlPrefix + "/users");
            subLink23.setLinkIcon("");
            crmSettingsLinknew.addChildLink(subLink21).addChildLink(subLink22).addChildLink(subLink23);
            headerLinks.add(crmSettingsLinknew);
        }
        RapidLinkModel userPersonalSettingsLinks = new RapidLinkModel(3);
        userPersonalSettingsLinks.setLinkType("HEADERLINK");
        userPersonalSettingsLinks.setLinkLabel(userModel.getDisplayName());
        userPersonalSettingsLinks.setLinkUrl("");
        userPersonalSettingsLinks.setLinkIcon("");
        RapidLinkModel subLink31 = new RapidLinkModel();
        subLink31.setLinkType("HEADERLINK");
        subLink31.setLinkLabel("LBL_MY_PREFERENCES");
        subLink31.setLinkUrl(String.valueOf(userModel.getPreference().get("detailViewUrl")));
        subLink31.setLinkUrl("/settings"); // :TODO will remove later.
        subLink31.setLinkIcon("");
        RapidLinkModel subLink32 = new RapidLinkModel();
        RapidLinkModel subLink33 = new RapidLinkModel();
        subLink33.setLinkType("HEADERLINK");
        subLink33.setLinkLabel("LBL_SIGN_OUT");
        subLink33.setLinkUrl("/logout");
        subLink33.setLinkIcon("");
        userPersonalSettingsLinks.addChildLink(subLink31).addChildLink(subLink32).addChildLink(subLink33);
        headerLinks.add(userPersonalSettingsLinks);

        return headerLinks;
    }

    /**
     * Function to get the list of Script models to be included
     *
     * @param request HttpServletRequest
     * @return <Array> - List of Vtiger_JsScript_Model instances
     */
    @Override
    protected List<RapidJsScriptModel> getHeaderScripts(HttpServletRequest request) {
        List<RapidJsScriptModel> headerScriptInstances = super.getHeaderScripts(request);
        Map<String, List<RapidLinkModel>> headerScriptMap = getAppContextBean(RapidLinkModelProvider.class)
                .getAllByType(RapidLinkModelProvider.IGNORE_MODULE, new String[]{"HEADERSCRIPT"});
        if (headerScriptMap != null) {
            headerScriptMap.forEach((headerType, headerScripts) -> {
                for (RapidLinkModel headerScript : headerScripts) {
                    headerScriptInstances.add(RapidJsScriptModel.getInstanceFromLinkObject(headerScript));
                }
            });
        }

        return headerScriptInstances;
    }

    /**
     * Function to get the list of Css models to be included
     *
     * @param request HttpServletRequest
     * @return <Array> - List of Vtiger_CssScript_Model instances
     */
    @Override
    protected List<RapidCssScriptModel> getHeaderCss(HttpServletRequest request) {
        List<RapidCssScriptModel> headerCssInstances = super.getHeaderCss(request);

        Map<String, List<RapidLinkModel>> headerCssMap = getAppContextBean(RapidLinkModelProvider.class)
                .getAllByType(RapidLinkModelProvider.IGNORE_MODULE, new String[]{"HEADERCSS"});

        String selectedThemeCssPath = getThemeHandler(request).getStylePath("");
        // :TODO check the filename whether it is less or css and add relative less
        boolean isLessType = selectedThemeCssPath.contains(".less") ? true : false;
        RapidCssScriptModel cssScriptModel = new RapidCssScriptModel();
        cssScriptModel.setHref(selectedThemeCssPath);
        cssScriptModel.setRel(isLessType ? RapidCssScriptModel.LESS_REL : RapidCssScriptModel.DEFAULT_REL);
        headerCssInstances.add(cssScriptModel);

        if (headerCssMap != null) {
            headerCssMap.forEach((headerType, cssLinks) -> {
                for (RapidLinkModel cssLink : cssLinks) {
                    headerCssInstances.add(RapidCssScriptModel.getInstanceFromLinkObject(cssLink));
                }
            });
        }

        return headerCssInstances;
    }

    /**
     * Function to get the Announcement
     *
     * @return RapidBaseModel - Announcement
     */
    protected List<RapidBaseModel> getAnnouncement() {
        return new ArrayList<>(0);
    }
}
